/**
 * \file des.hpp
 * \brief Base process and device definitions headerfile
 * \author Wouter Caarls <w.caarls@tudelft.nl> 
 *
 * \verbatim
 * Copyright (c) 2005-7 Wouter Caarls, Delft University of Technology
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * \endverbatim
 */

#ifndef __DES_HPP
#define __DES_HPP

#include <ucontext.h>

#include <iostream>
#include <list>

#define TIME unsigned long long
#ifndef NULL
#define NULL 0
#endif
#define FIBER_STACK (128*1024)

class DESevent
{
public:
  typedef enum {EventNone, EventStart, EventSignal, EventAwake, EventSchedule} DESeventType;

public:
  DESevent(DESeventType type=EventNone, TIME time=0,
	   class DESsignal *signal=NULL, class DESprocess *process=NULL);

  friend bool operator==(const DESevent& a, const DESevent& b);
  friend bool operator<(const DESevent& a, const DESevent& b);
  friend bool operator>(const DESevent& a, const DESevent& b);
  friend std::ostream& operator<<(std::ostream &s, DESevent e);
  friend std::ostream& operator<<(std::ostream &s, DESevent::DESeventType t);

public:
  DESeventType            m_type;
  TIME                    m_time;
  DESsignal              *m_signal;
  DESprocess             *m_process;

private:
  unsigned long long int  m_order;
};

bool operator==(const DESevent& a, const DESevent& b);
bool operator<(const DESevent& a, const DESevent& b);
bool operator>(const DESevent& a, const DESevent& b);
std::ostream& operator<<(std::ostream &s, DESevent e);
std::ostream& operator<<(std::ostream &s, DESevent::DESeventType t);

class DESprocess
{
public:
  DESprocess(std::string name="unnamed", void *argument=NULL);
  virtual ~DESprocess();

  virtual void main(void *argument) = 0;

  void switchToProcessContext(DESevent event);
  void switchToMainContext();

  virtual DESevent wait(class DESsignal* signal);
  virtual DESevent wait(TIME time);
  virtual void dissipate(double energy);
  virtual void exit();

public:
  class DESdevice  *m_device;
  DESevent          m_lastEvent;
  void             *m_argument;
  ucontext_t        m_context;
  std::string       m_name;
  struct lt_symbol *m_facility;
};

class DESdevice
{
public:
  DESdevice(std::string name="unnamed") : m_name(name), m_energy(0), m_processing(0), m_facility(NULL) { }
  virtual ~DESdevice() { }

  virtual void processEvent(DESevent event) = 0;

  virtual DESevent wait(class DESprocess *process, class DESsignal* signal) = 0;
  virtual DESevent wait(class DESprocess *process, TIME time) = 0;
  virtual void dissipate(double energy);
  virtual void exit(class DESprocess *process) = 0;

public:
  std::string       m_name;
  double            m_energy;
  int               m_processing;
  struct lt_symbol *m_facility;
};

class DESsignal
{
public:
  typedef std::list<class DESprocess*>::iterator LI;

public:
  void signal();
  void addListener(class DESprocess* process);

public:
  std::list<class DESprocess*> m_listeners;
};

TIME DESrun(void);
void DESaddEvent(DESevent event);
TIME DESgetCurrentTime();
DESprocess *DESgetCurrentProcess();

void DESopenTrace(char *file);
struct lt_trace *DESgetTrace();
void DEScloseTrace(void);
int DESgetEvents(void);

#endif /* __DES_HPP */
